<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class BeautyController extends Controller
{
    //美妆商城分类列表
    public function getIndex(Request $request)
    {
    	if($request->input('keywords')){
    		$cate = DB::table('goodsort')
    		->where('goodsname','like','%'.$request->input('keywords').'%')
    		->select(DB::raw("*,concat(path,'-',id) as paths"))
    		->orderBy('paths')
    		->paginate(5);
    	}else{
        	$cate = DB::table('goodsort')
        		->select(DB::raw("*,concat(path,'-',id) as paths"))
        		->orderBy('paths')
        		->paginate(5);
    	}
    	$data = $request->all();
    	return view('admin.beauty.index',['cate'=>$cate,'data'=>$data]);
    }
	//添加分类
	public function getAdd()
	{
		$res = self::getCates();
		return view('admin.beauty.add',['cates'=>$res]);
	}
	// 执行添加
	public function postInsert(Request $request)
	{
		$gid = $request->input('gid');
		$goodsname = $request->input('goodsname');
		$data = $request->only(['gid','goodsname']);
		if($goodsname == ''){
			return back();
		}
		if($gid == 0){
			$data['path'] = '0';
		}else{
			$res = DB::table('goodsort')->where('id',$gid)->first();
			$data['path'] = $res->path.'-'.$gid;
		}
		$data['date'] = date('Y-m-d H:i:s',time());
		// 执行
		$res = DB::table('goodsort')->insert($data);
		if($res){
			return redirect('/admin/beauty/index')->with('success','添加成功');
		}else{
			return back()->with('error','添加失败');
		}
	}
	//删除
	public function getDelete($id)
	{
		$res = DB::table('goodsort')->where('gid',$id)->count();
		if($res){
			return back();
		}else{
			$r = DB::table('goodsort')->where('id',$id)->delete();
			if($r){
				return back()->with('success','删除成功');
			}else{
				return back()->with('error','删除失败');
			}
		}
	}
	//修改
	public function getEdit($id)
	{
		$catesinfo = DB::table('goodsort')->where('id',$id)->first();
		$cates = self::getCates();
		// dd($cates);
		return view('admin.beauty.edit',['catesinfo'=>$catesinfo,'cates'=>$cates]);
	}
	//执行修改
	public function postUpdate(Request $request)
	{
		$id = $request->input('id');
		$gid = $request->input('gid');
		$yid = $request->input('yid');
		$res = DB::table('goodsort')->where('gid',$id)->count();
		if($gid != $yid){
			if($res){
				return back();
			}
		}
		$data = $request->only(['gid','goodsname']);
		if($gid == 0){
			$data['path'] = '0';
		}else{
			$res = DB::table('goodsort')->where('id',$gid)->first();
			$data['path'] = $res->path.','.$gid;
		}
		$r = DB::table('goodsort')->where('id',$id)->update($data);
		if($r){
			return redirect('admin/beauty/index')->with('success','分类修改成功');
		}else{
			return back()->with('error','分类修改失败');
		}
	}
	//处理分类结果
    public static function getCates()
	{
		$res = DB::table('goodsort')
		    ->select(DB::raw("*,concat(path,'-',id) as paths"))
		    ->orderBy('paths')
		    ->get();
		//处理数据
		foreach($res as $k=>$v){
			$arr = explode('-',$v->path);
			$num = count($arr)-1;
			$res[$k]->goodsname = str_repeat('|--',$num).$v->goodsname;
		}
		return $res;
	}
}
